package com.winchain.chainbill.serfee.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.account.util.VirtualAcctUtil;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServiceFeeTranService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public AcctTxRec getDeductSerFeeTran(Object recObj, Object dctObj, String bizType) {
		String txCusId;
		String grpId;
		BigDecimal txAmt;
		String billId;
		if ("0".equals(bizType)) {
			CbRec rec = (CbRec) recObj;
			CbDct dct = (CbDct) dctObj;
			grpId = rec.getGrpId();
			txAmt = dct.getSerFee();
			billId = rec.getBillId();
			txCusId = dct.getSellerId();
		} else {
			TbRec rec = (TbRec) recObj;
			TbDct dct = (TbDct) dctObj;
			grpId = rec.getGrpId();
			txAmt = dct.getSerFee();
			billId = rec.getBillId();
			txCusId = dct.getSellerId();
		}
		if ((!ChainBillUtil.onlinePay()) || (!VirtualAcctUtil.onVirtual())) {
			grpId = null;
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setTxAmt(txAmt);
		txRec.setTxDt(DateUtil.getSystemDate());
		txRec.setTxTs(DateUtil.getSystemDateTime());
		txRec.setTxAbs("��������-������������������");
		txRec.setTxType("de_ser_fee");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");
		txRec.setTxOperator("System");
		txRec.setCurrCode(AssetCode.CNY.name());
		txRec.setTxChnCode("B");

		txRec.setOptType("qt_trd");

		txRec.setTxSts("I");

		AcctBas txAcct = this.billAcctService.getAcctBas(txCusId, null, "00");
		txRec.setTxAcctNo(txAcct.getAcctNo());
		txRec.setTxAcctNm(txAcct.getAcctNm());
		txRec.setTxBankNm(txAcct.getFiNm());

		String cpCusId = CommonDataUtil.getCurEntId();
		AcctBas cpAcct = this.billAcctService.getAcctBas(cpCusId, null, "00");
		txRec.setCpAcctNo(cpAcct.getAcctNo());
		txRec.setCpAcctNm(cpAcct.getAcctNm());
		txRec.setCpBankNm(cpAcct.getFiNm());

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), grpId, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), grpId, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());
		return txRec;
	}
}
